// import { menuList, menuTree, login, logout } from '@/api/module/user'
import {
    login
} from '@/api/module/user'
// import { createRouteByList, createRouteByTree } from '@/route/permission'
import {
    message
} from 'ant-design-vue'

const state = {
    token: sessionStorage.getItem('USER_TOKEN') != null ?
        sessionStorage.getItem('USER_TOKEN') : '',
    userInfo: sessionStorage.getItem('USER_INFO') != null ?
        JSON.parse(sessionStorage.getItem('USER_INFO')) : null,
    userRoutes: sessionStorage.getItem('USER_ROUTES') != null ?
        sessionStorage.getItem('USER_ROUTES') : [],
    userPowers: sessionStorage.getItem('USER_POWERS') != null ?
        sessionStorage.getItem('USER_POWERS') : [],
}

const mutations = {
    SET_USER_TOKEN(state, token) {
        if (token) {
            state.token = token
            sessionStorage.setItem('USER_TOKEN', token)
        } else {
            state.token = ''
            sessionStorage.removeItem('USER_TOKEN')
        }
    },
    SET_USER_INFO(state, userInfo) {
        state.userInfo = userInfo
        sessionStorage.setItem('USER_INFO', userInfo)
    },
    SET_USER_MENU(state, menuList) {
        if (menuList && menuList.length === 0) {
            state.userRoutes = []
            sessionStorage.removeItem('USER_ROUTES')
        } else {
            const finalMenu = menuList
            state.userRoutes = finalMenu
            sessionStorage.setItem('USER_ROUTES', JSON.stringify(finalMenu))
        }
    },
}

const actions = {
    setUserToken({
        commit
    }, token) {
        return new Promise((resolve) => {
            commit('SET_USER_TOKEN', token)
            resolve()
        })
    },
    async logout({
        commit
    }, msgFlag) {
        sessionStorage.removeItem('USER_TOKEN')
        if (msgFlag) {
            message.success('注销成功', 0.5)
        }
        commit('SET_USER_TOKEN')
        commit('SET_USER_MENU')
        window.location.reload()
        return Promise.resolve()
    },
    async login({
        commit
    }, data) {
        const response = await login(data)
        const {
            msg,
            code,
            data: userInfo,
        } = response.data
        const token = response.headers.authorization
        if (token) {
            commit('SET_USER_TOKEN', token)

        }
        if (code === 200) {
            delete userInfo.menuList
            delete userInfo.token
            commit('SET_USER_INFO', JSON.stringify(userInfo))
            return Promise.resolve({
                msg,
                userInfo
            })
        } else {
            return Promise.reject(msg)
        }
    },
    // async addUserRouteForArray({ state: { userRoutes }, commit }) {
    //     console.log(userRoutes)
    //     const { result: data } = await menuList()
    //     const dynamicRoutes = createRouteByList(data)
    //     commit('SET_USER_MENU', dynamicRoutes)
    // },
    // async addUserRouteForTree({ state: { userRoutes }, commit }) {
    //     console.log(userRoutes)

    //     const { result: data } = await menuTree()
    //     const dynamicRoutes = createRouteByTree(data)
    //     commit('SET_USER_MENU', dynamicRoutes)
    // },
}

export default {
    namespaced: true,
    mutations,
    actions,
    state,
}